package com.tbit.uqbike.object.pojo.dto;

import com.alibaba.fastjson.annotation.JSONField;
import com.tbit.uqbike.object.value.LoginInfo;
import com.tbit.common.entity.view.Result;
import lombok.*;
import org.apache.commons.lang3.StringUtils;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.util.Objects;
import java.util.Optional;

/**
 * @author yee_
 * @version 1.0
 * @date 2023/8/11 13:59
 */

@Data
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Builder(toBuilder = true)
public class RechargeConfigDTO {

    /**
     * 赠送配置id - 修改时必传
     */
    private Integer rechargeConfigId;

    @NotNull(message = "ver.accountId.notNull")
    private Integer accountId;
    /**
     * 充值金额
     */
    @NotNull(message = "请填写充值金额")
    @Min(value = 1, message = "充值金额错误，请输入1-100000以内整数")
    @Max(value = 10000000, message = "充值金额错误，请输入1-100000以内整数")
    private Integer baseMoney;

    private Integer giveMoney;
    /**
     * 优惠劵名称
     */
    private String couponName;
    /**
     * 优惠劵id
     */
    private Integer couponId;
    /**
     * 优惠劵赠送数量
     */
    private Integer couponCount;
    /**
     * 优惠劵有效天数
     */
    private Integer validDays;
    /**
     * 状态 1-启用 0-停用
     */
    @NotNull(message = "请选择状态")
    @Min(value = 0, message = "状态错误，请选择启用或停用")
    @Max(value = 1, message = "状态错误，请选择启用或停用")
    private Integer status;

    /**
     * 充值前提示 0：关闭 1：开启
     */
    private Integer hintType;

    private String remark;

    @JSONField(serialize = false)
    private LoginInfo loginInfo;


    public Result<String> businessLogicCheck() {
        if (this.couponId != null) {
            if (this.couponCount < 0) {
                return Result.error("优惠劵赠送数量不能小于零");
            }
            if (this.validDays < 0) {
                return Result.error("优惠劵有效天数不能小于零");
            }
        }
        if (this.giveMoney != null) {
            if (this.giveMoney < 0) {
                return Result.error("赠送金额错误，请输入0-100000以内整数");
            }
            if (this.giveMoney > 10000000) {
                return Result.error("赠送金额错误，请输入0-100000以内整数");
            }
        }
        return Result.success();
    }

    public String buildDetails() {
        StringBuilder builder = new StringBuilder();
        if (this.giveMoney != null && this.giveMoney > 0) {
            builder.append("赠送金额").append(Double.parseDouble(this.giveMoney.toString()) / 100).append("元;");
        }
        if (this.couponId != null) {
            builder.append("赠送优惠劵[").append(this.couponName).append("]*")
                    .append(this.couponCount).append("张,有效期")
                    .append(this.validDays).append("天;");
        }
        return Optional.of(builder.toString())
                .filter(StringUtils::isNotBlank)
                .map(x -> x.substring(0, x.length() - 1))
                .orElse("-");
    }

    public boolean haveGive() {
        return (Objects.nonNull(this.giveMoney) && this.giveMoney > 0) || this.couponId != null;
    }

}


